package com.ftg.learn.rbac.service.impl;

import com.ftg.learn.rbac.entity.Role;
import com.ftg.learn.rbac.mapper.RoleMapper;
import com.ftg.learn.rbac.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Guobt
 * @since 2022-02-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Map<String, Object>> showPermissionForRoleId(Long rid) {
        return roleMapper.showPermissionForRoleId(rid);
    }


    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean permissionGroup(Long rid,List<Long> pid) {
        roleMapper.delPermissionForRoleId(rid);
        for (Long aLong : pid) {
            roleMapper.savePermissionForRoleId(rid,aLong);
        }
        return true;
    }

}
